package com.edu.zju.scs.panacea.mapper.plan;

import com.edu.zju.scs.panacea.entity.plan.Plan;
import com.edu.zju.scs.panacea.entity.plan.Task;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author: Yue Liang
 * @Date: 2019-01-07 18:28
 */
@Repository
public interface TaskMapper {
    @Select("SELECT * FROM plan_task WHERE id = #{id}")
    @Results(id="taskMap", value={
            @Result(id=true,property="id",column="id"),
            @Result(property="planId",column="plan_id"),
            @Result(property="locId",column="loc_id"),
            @Result(property="sellerId",column="seller_id"),
            @Result(property="startDate",column="start_date"),
            @Result(property="endDate",column="end_date"),
            @Result(property="binLaunchTime",column="bin_launch_time"),
            @Result(property="state",column="state")})
    Task selectTask(Long id);

    @Select("SELECT * FROM plan_task")
    @ResultMap(value = "taskMap")
    List<Task> selectAllTask();

    @Delete("DELETE FROM plan_task where id = #{id}")
    long deleteTask(Long id);

    @Delete("DELETE FROM plan_task where plan_id = #{planId}")
    long deleteTaskByPlan(Long planId);

    @Insert("INSERT INTO plan_task(plan_id, loc_id, start_date, end_date, bin_launch_time, state) VALUES" +
            " (#{planId}, #{locId}, #{startDate}, #{endDate}, #{binLaunchTime}, 0)")
    @Options(useGeneratedKeys=true)
    long insertTask(Task task);

    @Update({ "update plan_task set " +
            "plan_id = #{planId}, " +
            "loc_id = #{locId}, " +
            "start_date = #{startDate}, " +
            "end_date = #{endDate}, " +
            "bin_launch_time = #{binLaunchTime}, " +
            "state = #{state} " +
            "where id = #{id}" })
    long updateTask(Task task);

    @Select("SELECT id FROM plan_task where plan_id = #{planId}")
    List<Long> selectTasksByPlan(Long planId);

}
